# 测试代码

# 一.测试函数


def get_formatted_name(first, last):
    """Generate a neatly formatted full name."""
    full_name = first + ' ' + last
    return full_name.title()


# print("Enter 'q' at any time to quit.")
# while True:
#     first = input("\nPlease give me a first name: ")
#     if first == 'q':
#         break
#     last = input("Please give me a last name: ")
#     if last == 'q':
#         break
#     formatted_name = get_formatted_name(first, last)
#     print("\tNeatly formatted name: " + formatted_name + '.')


# 单元测试以及测试用例（Python 标准库中的模块 unittest 提供了代码测试工具）
# 单元测试 用于核实函数的某个方面没有问题；
# 测试用例 是一组单元测试，这些单元测试一起核实函数在各种情形下的行为都符合要求。

# 二.编写测试用例
import unittest


# 需要继承unittest模块中的TestCase类
# class NamesTestCase(unittest.TestCase):
#     def test_first_last_name(self):
#         """ 能够正确地处理像 Janis Joplin 这样的姓名吗？ """
#         formatted_name = get_formatted_name('janis', 'joplin')
#         # 一个 断言 方法（用于核实得到的接过是否和期望的相一致）
#         self.assertEqual(formatted_name, 'Janis Joplin')


# 下面这条语句让Python运行这个文件中的测试
# unittest.main()

# 三.测试类
# 以下为Python中的六大测试用例
# 1.assertEqual(a, b) 核实a == b
# 2.assertNotEqual(a, b) 核实a != b
# 3.assertTrue(x) 核实x 为True
# 4.assertFalse(x) 核实x 为False
# 5.assertIn(item , list ) 核实 item 在 list 中
# 6.assertNotIn(item , list ) 核实 item 不在 list 中


class AnonymousSurvey:
    """ 收集匿名调查问卷的答案 """
    def __init__(self, question):
        """ 存储一个问题，并为存储答案做准备 """
        self.question = question
        self.responses = []

    def show_question(self):
        """ 显示调查问卷 """
        print(self.question)

    def store_response(self, new_response):
        """ 存储单份调查答卷 """
        self.responses.append(new_response)

    def show_results(self):
        """ 显示收集到的所有答卷 """
        print("Survey results:")
        for response in self.responses:
            print('- ' + response)


class TestAnonymousSurvey(unittest.TestCase):
    """ 针对 AnonymousSurvey 类的测试 """
    def test_store_three_responses(self):
        """ 测试三个答案会被妥善地存储 """
        question = "What language did you first learn to speak?"
        my_survey = AnonymousSurvey(question)
        responses = ['English', 'Spanish', 'Mandarin']
        for response in responses:
            my_survey.store_response(response)
        for response in responses:
            self.assertIn(response, my_survey.responses)

    def test_store_single_response(self):
        """ 测试单个答案会被妥善地存储 """
        question = "What language did you first learn to speak?"
        my_survey = AnonymousSurvey(question)
        my_survey.store_response('English')
        self.assertIn('English', my_survey.responses)


unittest.main()

# 四.方法setUp()：只需创建这些对象一次，并在每个测试方法中使用它们


class TestAnonymousSurvey1(unittest.TestCase):
    """ 针对 AnonymousSurvey 类的测试 """

    def setUp(self):
        """创建一个调查对象和一组答案，供使用的测试方法使用"""
        question = "What language did you first learn to speak?"
        self.my_survey = AnonymousSurvey(question)
        self.responses = ['English', 'Spanish', 'Mandarin']

    def test_store_three_responses(self):
        """ 测试三个答案会被妥善地存储 """
        for response in self.responses:
            self.my_survey.store_response(response)
        for response in self.responses:
            self.assertIn(response, self.my_survey.responses)

    def test_store_single_response(self):
        """ 测试单个答案会被妥善地存储 """
        question = "What language did you first learn to speak?"
        my_survey = AnonymousSurvey(question)
        my_survey.store_response('English')
        self.assertIn('English', my_survey.responses)

